function formatCurrency(num) {
	
	num = num.toString().replace(/\$|\,/g,'');
	
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	
	if(cents<10)
	   cents = "0" + cents;
	
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	   num = num.substring(0,num.length-(4*i+3))+','+
	
	num.substring(num.length-(4*i+3));
	
	return (((sign)?'':'-') + num + '.' + cents);
}

$(document).ready(function(){
    
    function renderToOutput(str)
    {
		var i = 1;
		
		$('.'+str+'_resp').each(function() {		
			 $('.'+str+i).val(this.value);
			 i++;
		});
					
		i=1;						
	}
    
	$('#btnprocess').click(function() {
	   
	   var location = $("#location").val();
	   var container_size = $("#container_size").val();
	   var tariffpack = $("#tariffpack").val();
	   var qty = $("#qty").val();
	   var total_transaction = 0;
	   var SessionVar = $("#SessionVar").val();
       
       if(qty == 0 || qty == '')
       {
		  qty = 1;
	   }
       
		$('.tariffgrid').empty().append('recalculating .....');
		$.ajax({
			//url: "/"+baseUrl+"/OrderPraIns/chargeProcess/?service=12&depo="+depo+"&"+container_size.join('&')+qty.join('&'),
			url: "ais_transaction_dn_form_response.php?tariffpack="+tariffpack+"&qty="+qty+"&SessionVar="+SessionVar,
			success:function(result){
						
						var row = parseInt($('#row_resp').val()); 		
						var i = 1;
						var outputHtml = "";
						
												
						$('.tariffgrid').empty().append(result);
						
						renderToOutput('memorial_id');
						renderToOutput('urut');
						renderToOutput('uraian');
						renderToOutput('jumlah');
						renderToOutput('skada');
						renderToOutput('noorder');
						renderToOutput('noref');
						renderToOutput('kdact');
							
						/*	
						$('.uraian_resp').each(function() {		
							 outputHtml="<input type='text' value='"+this.value+"' name='uraian"+i+"'>"; //need advice
							 $('.uraian'+i).val(this.value);
							 i++;
						});
						
												
						$('.uraian_ans').html(outputHtml);
						i=1;						
						outputHtml="";
						*/
						
						
						
						
						$('.amount_hdn').each(function() {							
							total_transaction+= parseInt(this.value);
						});
						
						$('#total_trans').val(formatCurrency(total_transaction));
						
						
					}
		});
	   
	   
	});


    		
});






